/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ngr.KiKi.appreciator.view;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import ngr.KiKi.appreciator.JFrameMain;
import ngr.KiKi.appreciator.data.Student;
import ngr.KiKi.appreciator.data.Utils;

/**
 *
 * @author KiKi
 */
public class JPanelClassView extends javax.swing.JPanel
{
	
	private Map<Student, Integer> map;
	
	private final JFrameMain parent;
	private DefaultTableModel model;

	/**
	 * Creates new form JPanelClassView
	 */
	public JPanelClassView (JFrameMain p)
	{
		initComponents ();
		
		parent = p;
		
		init ();
	}
	
	private void init ()
	{
		map = new HashMap<> ();
		model = (DefaultTableModel) jTableClass.getModel ();
		jTableClass.setSelectionModel (new ForcedListSelectionModel ());
		jTableClass.setDefaultRenderer (String.class, new MultiLineTableCellRenderer ());
		jTableClass.setCursor (new Cursor (Cursor.HAND_CURSOR));
		jTableClass.setToolTipText ("Copier appréciation");
		
		MouseAdapter mouseAdapter = new MouseAdapter ()
		{
			private int[] previous = new int[]
			{
				-1, -1
			};
			
			private int[] cellAtPoint (Point p)
			{
				return new int[]
				{
					jTableClass.rowAtPoint (p), jTableClass.columnAtPoint (p)
				};
			}
			
			@Override
			public void mouseMoved (MouseEvent me)
			{
				int[] cell = cellAtPoint (me.getPoint ());
				
				if (cell[0] != previous[0])
				{
					((MultiLineTableCellRenderer) jTableClass.getDefaultRenderer (String.class)).setHighlightedRow (cell[0]);
					repaint ();
				}
				
				previous = cell;
			}
			
			@Override
			public void mouseClicked (MouseEvent me)
			{
				if (me.getButton () == MouseEvent.BUTTON1)
				{
					int[] cell = cellAtPoint (me.getPoint ());
					if (cell[0] != -1)
					{
						String text = (String) model.getValueAt (cell[0], 1);
						Utils.sendToClipboard (text);
						parent.setStatus (text);
						parent.switchStudent (cell[0]);
					}
				}
			}
			
			@Override
			public void mouseExited (MouseEvent me)
			{
				((MultiLineTableCellRenderer) jTableClass.getDefaultRenderer (String.class)).setHighlightedRow (-1);
				repaint ();
			}
			
			@Override
			public void mouseReleased (MouseEvent me)
			{
				mouseClicked (me);
			}
		};
		
		jTableClass.addMouseListener (mouseAdapter);
		jTableClass.addMouseMotionListener (mouseAdapter);
	}
	
	public void load (ArrayList<Student> list)
	{
		clear ();
		
		list.stream ().forEach ((st) ->
		{
			model.addRow (new String[]
			{
				st.getfullname (), st.getAppreciation ()
			});
			map.put (st, model.getRowCount () - 1);
		});
		
	}
	
	public void clear ()
	{
		map.clear ();
		model.setRowCount (0);
	}
	
	public void set (Student st, String a)
	{
		model.setValueAt (a, map.get (st), 1);
	}
	
	public class ForcedListSelectionModel extends DefaultListSelectionModel
	{
		
		public ForcedListSelectionModel ()
		{
			setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
		}
		
		@Override
		public void clearSelection ()
		{
		}
		
		@Override
		public void removeSelectionInterval (int index0, int index1)
		{
		}
	};

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTableClass = new javax.swing.JTable();

        jScrollPane2.setBorder(null);

        jTableClass.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {
                "Elève", "Appréciation"
            }
        )
        {
            Class[] types = new Class []
            {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean []
            {
                false, false
            };

            public Class getColumnClass(int columnIndex)
            {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex)
            {
                return canEdit [columnIndex];
            }
        });
        jTableClass.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jTableClass);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableClass;
    // End of variables declaration//GEN-END:variables
}
